package com.springMVCStudy.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MyInterceptor implements HandlerInterceptor {

    /*
    执行handler之前调用的方法，应用如编码格式设置、登录保护、权限处理
    返回值，true放行，false拦截
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle: " + "request = " + request + ", response = " + response + ", handler = " + handler);
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    /*
    当handler执行完毕后，触发的方法，没有拦截机制。应用如结果处理、敏感词检查
    第四个参数modelAndView指要返回的视图和共享域对象
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle: " + "request = " + request + ", response = " + response + ", handler = " + handler + ", modelAndView = " + modelAndView);
    }

    /*
    整体都处理完毕后触发，也没有拦截机制
    第四个参数ex是handler报错后的异常对象

     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterHandle: " + "request = " + request + ", response = " + response + ", handler = " + handler + ", ex = " + ex);
    }
}
